<?php
echo "<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>
<html xmlns='http://www.w3.org/1999/xhtml' lang='es' xml:lang='es'>
<head>
    <title>Contador de visitas</title>
    <meta http-equiv='content-type' content='text/html; charset=utf-8' />
    <meta name='author' content='Antonio Aguilera Malagón' />
    <meta name='description' content='Ejercicio notable: Contador de visitas' />
</head>
<body>
	<form action='contador.php' method='get'>
	<fieldset>
        <input type='submit' name='reset' value='Resetear' />
        <input type='submit' name='add' value='Incrementar' />
        <input type='submit' name='set' value='Asignar valor' />
        <input type='text' name='new_value' value='' />
    </fieldset>
    </form>";

function mostrarError($client, $error) {
	echo "<b>Error</b>: " . $error->faultstring;
	echo "<br />";
	echo htmlspecialchars($client->__getLastResponse(), ENT_QUOTES);
}

$fichero = "contador.txt";
#$location = "http://aaguilera.bugs3.com/contador_ws.php";
#$location = "http://localhost/aaguilera/contador_ws.php";
$location = "https://code.google.com/p/aaguilera-contador-visitas/contador_ws.php";
$client = new SoapClient(null, array('location' => $location, 'uri' => '', 'trace' => 1));

try {
	if (isset($_GET['reset'])) {
		$client->resetearContador($fichero);
	} else if (isset($_GET['add'])) {
		$client->incrementarContador($fichero);
	} else if (isset($_GET['set'])) {
		$client->setContador($fichero, $_GET['new_value']);
	}
	echo "<p><br /><b>Contador</b>: " . $client->getContador($fichero) . "</p>";
} catch (SoapFault $error) {
	mostrarError($client, $error);
}

echo "
<p>
	<a href='http://validator.w3.org/check?uri=referer'>
		<img src='http://www.w3.org/Icons/valid-xhtml10' alt='Valid XHTML 1.0 Strict' height='31' width='88' />
	</a>
</p>
</body>
</html>";
?>